import TWEEN from "@tweenjs/tween.js";
import { Blood_BarsType } from "./utils";
import * as THREE from "three";
import { scene } from "../components/sence"
import { Material } from "three";

export const AnimalViews:Array<AnimalView> = []
export const leftAnimals:Array<Animal> = []
export const rightAnimals:Array<Animal> = []
export class Animal {

    images:Array<THREE.Texture>
    ATK:number
    column:number=0
    size:number=2
    attack_interval:number
    attack_distance:number = 0.1;
    blood_volume:number
    blood_bars:Blood_BarsType
    speed:number
    owned:"right"|"left"
    constructor(images:Array<THREE.Texture>,ATK:number,blood_volume:number,blood_bars:Blood_BarsType,speed:number,attack_interval:number,size?:number,animation_time?:number){
        this.images = images
        this.ATK = ATK
        this.blood_volume = blood_volume
        this.blood_bars = blood_bars
        this.speed =speed
        this.column = 0;
        AnimalViews.push(new AnimalView(this,animation_time))
        if(this.owned=="left"){
            leftAnimals.push(this)
        }else{
            rightAnimals.push(this)
        }
    }
    initColumn(){

        // for()
        
        return 0
    }
}


class AnimalView{
    animalBox: THREE.Mesh<THREE.BoxGeometry, THREE.MeshBasicMaterial, THREE.Object3DEventMap>
    animal:Animal
    _nowimg:number=0
    animation_time:number=500;
    _lastime:number=Date.now()
    get isPlayAnimation():boolean{
        if(Date.now()-this._lastime>this.animation_time){
            this._lastime=Date.now()
            return true
        }
        return false
    }

    get nowimg ():number{
        if(this._nowimg<this.animal.images.length-1)
            this._nowimg+=1;
        else 
            this._nowimg=0;
        return this._nowimg
    }

    constructor(animal:Animal,animation_time?:number){
        this.animal = animal
        if(animation_time)
        this.animation_time = animation_time
        const buildingMaterial = new THREE.MeshBasicMaterial({map: animal.images[0]})
        const box = new THREE.Mesh(
            new THREE.BoxGeometry(0.4,0.4,0.4),
            buildingMaterial
        )
        this.animalBox=box
        scene.add(box)
        // console.log(this.animalBox.position.set(1,0.1,0))
        
    }

    flushed=()=>{
        moveto(this.animalBox,{z:this.animalBox.position.z+0.1} ,1000)
        this.animalBox.material.map = this.animal.images[this.nowimg]
        // moveto(this,this.position)
    }
}



export function moveto(pos,position,time){
    const tween = new TWEEN.Tween(pos.position);
    tween.to(position,time);
    tween.start();
}